package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Reference
    private MemberService memberService;


    @Autowired
    private JedisPool jedisPool;
    @RequestMapping("/findPage")
    @PreAuthorize("hasAnyAuthority('MEMBER_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult page = memberService.findPage(queryPageBean);
        return page;
    }

    @RequestMapping("/findManager")
    public Result findManager() {
        try {
            List<String> list = memberService.findManager();
            System.out.println(list);
            return new Result(true, MessageConstant.QUERY_HEALTHMANAGER_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_HEALTHMANAGER_FAIL);
        }
    }

    /*查询全部*/
    @RequestMapping("/findAll")
    @PreAuthorize("hasAnyAuthority('MEMBER_QUERY')")
    public Result findAll() {
        try {
            List<String> list = memberService.findAll();
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
        }
    }
    @RequestMapping("/add")
    @PreAuthorize("hasAnyAuthority('MEMBER_ADD')")
    public Result add(@RequestBody Member member) {
        try {
            memberService.add(member);
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
    }

    @RequestMapping("/delete")
    @PreAuthorize("hasAnyAuthority('MEMBER_DELETE')")
    public Result delete(Integer id) {
        try {
            memberService.delete(id);
            return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
        }
    }

    @RequestMapping("/edit")
    @PreAuthorize("hasAnyAuthority('MEMBER_EDIT')")
    public Result edit(@RequestBody Member member) {
        try {
            memberService.edit(member);
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }
    }

    @RequestMapping("/findById")
    @PreAuthorize("hasAnyAuthority('MEMBER_EDIT')")
    public Result findById(Integer id) {
        System.out.println(id);
        try {
            Member member = memberService.findById(id);
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS, member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
        }
    }

    @RequestMapping("/memberCount")
    public Result memberCount() {
        try {
            Map map = memberService.memberCount();
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_MEMBER_FAIL);
        }
    }


    /*查询会员体检上传报告*/
    @RequestMapping("/getMemberReport")
    @PreAuthorize("hasAnyAuthority('REPORT_QUERY')")
    public PageResult getMemberReport(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = memberService.getMemberReport(queryPageBean);
        return pageResult;
    }

    /*上传体检报告*/
    @RequestMapping("/reportUpload")
    @PreAuthorize("hasAnyAuthority('REPORT_UPLOAD')")
    public Result reportUpload(@RequestParam("excelFile") MultipartFile excelFile) {
        //将文件传到七牛云上去
        String originalFilename = excelFile.getOriginalFilename();//原始文件名
        int index = originalFilename.lastIndexOf(".");
        String suffix = originalFilename.substring(index);
        String fileName = UUID.randomUUID().toString() + suffix;
        System.out.println("fileName = " + fileName);
        //将图片保存到七牛云
        try {
            QiniuUtils.upload2Qiniu(excelFile.getBytes(), fileName);
            //文件上传成功后，需要将文件名称保存到redis中
            jedisPool.getResource().sadd(RedisConstant.MEMBER_FILE_REPORT, fileName);
            //将文件名返回,用作提交到后台使用
            return new Result(true, MessageConstant.REPORT_UPLOAD_SUCCESS, fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.REPORT_UPLOAD_FAIL);
        }
    }

    @RequestMapping("/uploadReport")
    @PreAuthorize("hasAnyAuthority('REPORT_UPLOAD')")
    public Result uploadReport(@RequestBody Map map) {
        String imgName = (String) map.get("imgName");
        if (imgName== null || "".equals(imgName)){
            return new Result(false,MessageConstant.PDF_REPORT_UPLOAD_FAIL);
        }
        try {
            memberService.uploadReport(map);
            return new Result(true, MessageConstant.PDF_REPORT_UPLOAD_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PDF_REPORT_UPLOAD_FAIL);
        }
    }

    @RequestMapping("/deleteReport")
    @PreAuthorize("hasAnyAuthority('REPORT_DELETE')")
    public Result deleteReport(Integer memberId, Integer orderId) {
        try {
            memberService.deleteReport(memberId, orderId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.REPORT_DELETE_FAIL);
        }
        return new Result(true, MessageConstant.REPORT_DELETE_SUCCESS);
    }

}
